<?php
  include_once 'global.php';
/*
 * 从数据库读取日志
 */
  $sql="SELECT * FROM `myblog` ORDER BY `blogID` DESC limit 0,4;";
  $result=$db->query($sql);
  $myblogs=array();
  $blog_ID=array();
  while($row=$db->fetch_assoc($result)){
  	$myblogs[]=$row;
  	$blog_ID[]=$row['blogID'];
  }
  //print_r($myblogs);
/*
 * 从数据库读取日志end
 */ 
  /************************************************************************/
/*
 * 读取日志评论数
 */
  $count_comment=array();
  for($i=0;$i<count($blog_ID);$i++){
  	$sql="select * from `comment` where blogID='$blog_ID[$i]';";
  	$result=$db->query($sql);
  	if($db->db_num_rows($result)){
  	$count_comment[]=$db->db_num_rows($result);}
  	else $count_comment[]=0;
  }
  for($i=0;$i<count($count_comment);$i++){
  	$myblogs[$i][count_comment]="$count_comment[$i]";
  }
  //print_r($myblogs);
/*
 * 读取日志评论数end
 */
  /************************************************************************/
/*
 * 从数据库读取留言
 */
  $sql="SELECT * FROM `remsg` ORDER BY `msg_ID` DESC limit 0,5;";
  $result=$db->query($sql);
  $remsg=array();
  while($row=$db->fetch_assoc($result)){
  	$remsg[]=$row;
  }
/*
 * 从数据库读取留言end
 */
  /************************************************************************/
/*
 * 判断其他登录用户是否关注博主微博
 */
  if(isset($_GET['followid'])&&!empty($_GET['followid'])){
  	$c = new WeiboClient( WB_AKEY , 
                      WB_SKEY , 
                      $_SESSION['last_key']['oauth_token'] , 
                      $_SESSION['last_key']['oauth_token_secret']  );
   //关注用户的id
   $u_id = intval($_GET['followid']);
   $me = $c->verify_credentials();
   $isfriend=$c->oauth->get('http://api.t.sina.com.cn/friendships/exists.json?user_a='.$me['id'].'&user_b='.$u_id);
   if($me['id']==$u_id){
   	echo "<script>alert('您不能关注自己！');</script>";
   }
   
   else if ($isfriend){
   	echo "<script>alert('您已经关注过他！');</script>";
   }
   else {
   $msg = $c->follow($u_id);
   if (isset($msg['screen_name'])){
	  echo "<script>alert('关注成功！');</script>";
     }
  }
  }
  /*
 * 判断其他登录用户是否关注博主微博end
 */
  $smarty->assign('url',$url);
  $smarty->assign('name','首页');
  
  $smarty->assign('myblogs',$myblogs);
  $smarty->assign('remsg',$remsg);
  
  //$smarty->assign('count_comment',$count_comment);
  $smarty->display('index.html');
  //$smarty->display('header.html');
  //echo "输出".$_SESSION['last_key']['oauth_token'];
?>